<?php

namespace  App\Http\Controllers\V1;

use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Http\Controllers\Controller;
use App\Models\Factory\SmsFactory;
use App\Models\Factory\UserFactory;
use App\Services\Core\Sms\Dahan\DahanService;
use Illuminate\Http\Request;
/**
 * Class GeetesController
 * @package App\Http\Controllers\V1
 * 短信
 */
class SmsController extends  Controller
{

    /**
     * 发送短信
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function SendCode(Request $request)
    {
        // 接收参数
        $mobile = $request->input('phone');

        // 检查用户是否存在
        $user = UserFactory::getUserByMobile($mobile);

        // 如果不存在的话，则向用户主表中插入一条数据，
        if (!empty($user) &&  !empty($user->password)) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(20000), 20000);

        }

        $user =UserFactory::addUser($mobile, '0');
        //        // 检查一分钟是否有重复发送
        $check = SmsFactory::checkCodeExistenceTime($mobile);

        if (!$check) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(1201), 1201);
        }

        $ipNums = SmsFactory::limitIp();

        if ($ipNums >= 18) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(7109), 7109);
        }
        $mobileNums = SmsFactory::limitMobile($mobile);

        if ($mobileNums >= 6) {
            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(7109), 7109);
        }




//         发送短信
        $re = DahanService::i()->send($mobile);

//         返回结果
        if (!$re) {
            $data['isSucess'] = "0";
            return RestResponseFactory::ok($data, RestUtils::getErrorMessage(1205), 1205);
        }

        $data['isSucess'] = "1";
        return RestResponseFactory::ok($data);
    }



    /**
     * 忘记密码发送短信
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPwdCode(Request $request)
    {
        // 接收参数
        $mobile = $request->input('phone');

        // 检查用户是否存在
        $user = UserFactory::getUserByMobile($mobile);


        // 如果不存在的话，则向用户主表中插入一条数据，
        if ($user) {
            $user =UserFactory::addUser($mobile, '0');
            //        // 检查一分钟是否有重复发送
            $check = SmsFactory::checkCodeExistenceTime($mobile);

            if (!$check) {
                return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(1201), 1201);
            }

            $ipNums = SmsFactory::limitIp();

            if ($ipNums >= 18) {
                return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(7109), 7109);
            }
            $mobileNums = SmsFactory::limitMobile($mobile);

            if ($mobileNums >= 6) {
                return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(7109), 7109);
            }

//         发送短信
            $re = DahanService::i()->send($mobile);

//         返回结果
            if (!$re) {
                $data['isSucess'] = "0";
                return RestResponseFactory::ok($data, RestUtils::getErrorMessage(1205), 1205);
            }

            $data['isSucess'] = "1";
            return RestResponseFactory::ok($data);
        }

        return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(20002), 20002);

    }


}